﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.OleDb;

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Common;
using NLBS.Framework;

namespace NLBS.Blog
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Clear();
            if (Request.IsAuthenticated)
            {
                if (Request.QueryString["s"] != null)
                {
                    HttpPostedFile file = Request.Files[0];
                    string fileName = Path.GetFileName(file.FileName);
                    string fullName = Server.MapPath("upload/") + fileName;
                    file.SaveAs(fullName);
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                }
            }
            else
            {
                Response.Clear();
                Response.Write("请登录");
            }
           //Response.End();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //(是否允许上传,上传文件大小,路径,文件后缀名)
            BlogInfo info=BlogInfo.Instance();
            if (info.EnableUpLoad == 0)
            {
                show("不允许上传");
                return;
            }

            HttpPostedFile file = Request.Files[0];
            if (file.ContentLength > info.UpLoadSize)
            {
                show("上传文件过大");
                return;
            }
            if (!checkuploadType())
            {
                show("文件格式不符合");
                return;
            }
            string fileName = Path.GetFileName(file.FileName);
            string fullName = Server.MapPath(info.UploadPath) + fileName;
            file.SaveAs(fullName);
            Label1.Text = "文件已成功上传。";
            Panel1.Visible = false;
            Panel2.Visible = true;
        }

        private bool checkuploadType()
        {
            string uploadType = Path.GetExtension(Request.Files[0].FileName);
            string[] systemuploadType = BlogInfo.Instance().UploadType.Split(new char[] { ',' });
            bool enableuploadType = false;
            foreach (string item in systemuploadType)
            {
                if (uploadType.ToLower().Substring(1) == item.ToLower())
                {
                    enableuploadType = true;
                    return enableuploadType;
                }
            }
            return enableuploadType;
        }

        private void show(string msg)
        {
            Label1.Text = msg;
            Panel1.Visible = false;
            Panel2.Visible = true;
        }

        
    }
}
